package kd.hc.ecco;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.BillShowParameter;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.MulBasedataDynamicObjectCollection;
import kd.bos.dataentity.metadata.clr.DataEntityPropertyCollection;
import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
import kd.bos.entity.datamodel.IBillModel;
import kd.bos.entity.datamodel.RowDataEntity;
import kd.bos.entity.datamodel.events.AfterAddRowEventArgs;
import kd.bos.entity.datamodel.events.AfterDeleteRowEventArgs;
import kd.bos.entity.datamodel.events.BeforeAddRowEventArgs;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.property.EntryProp;
import kd.bos.entity.property.entryfilter.EntryFilterItemInfo;
import kd.bos.entity.property.entryfilter.EntryQueryParam;
import kd.bos.exception.KDBizException;
import kd.bos.form.CloseCallBack;
import kd.bos.form.ShowType;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.Label;
import kd.bos.form.control.TreeEntryGrid;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.MulBasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.hc.utils.BusinessDataServiceHelperExt;
import kd.hc.utils.ShowParameterUtil;
import kd.sdk.plugin.Plugin;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 单据界面
 */
public class TargetCostBillPlugIn extends AbstractBillPlugIn implements Plugin, BeforeF7SelectListener {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        BasedataEdit travelbills = this.getView().getControl("hc95_cost_item_en");//目标成本项
        travelbills.addBeforeF7SelectListener(this);
    }

    @Override
    public void afterLoadData(EventObject e) {
        super.afterLoadData(e);
        onlyFiledLock();
        handleSumRow();
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        OperationResult operationResult = e.getOperationResult();
        String operateKey = e.getOperateKey();
        if (operationResult != null) {
            boolean success = operationResult.isSuccess();
            if (success) {
                switch (operateKey) {
                    case "dofastaddline":
                        ShowParameterUtil.showFormShowParameter("新增成本类型", this.getView(), this.getModel(), "hc95_cost_type_autotree", ShowType.Modal, new CloseCallBack(this, operateKey));
                        break;
                    case "dochage":
                        dochage();
                        break;
                }
            }
        }
    }

    public void dochage() {
        Boolean isExitPk = false;
        ArrayList<String> selectEntryIds = new ArrayList<>();
        DynamicObjectCollection entryentity = this.getModel().getEntryEntity("hc95_tree_entryentity");
        TreeEntryGrid entryGrid = this.getControl("hc95_tree_entryentity");
        int[] selectRows = entryGrid.getSelectRows();
        if (selectRows != null && selectRows.length > 0) {
            for (int selectRow : selectRows) {
                DynamicObject rowSelect = entryentity.get(selectRow);
                String longId = rowSelect.getString("hc95_longid_en");
                String id = rowSelect.getString("id");
                if (selectEntryIds.contains(id)) {
                    continue;
                }
                if (StringUtils.isNotBlank(longId)) {
                    List<String> ids = entryentity.stream()
                            .filter(f1 -> f1.getString("hc95_longid_en").startsWith(longId))
                            .filter(f2 -> f2.getBoolean("hc95_is_leaf"))
                            .map(m1 -> m1.getString("id")).collect(Collectors.toList());
                    selectEntryIds.addAll(ids);
                }
            }
        }
        List<String> selectEntryIdsDis = selectEntryIds.stream().distinct().collect(Collectors.toList());
        IBillModel billModel = (IBillModel) this.getModel();
        BillShowParameter billShowParameter = new BillShowParameter();
        List<QFilter> qFilterList = new ArrayList<QFilter>();
        qFilterList.add(new QFilter("hc95_mbcbd_id", QCP.equals, billModel.getPKValue()));
        qFilterList.add(new QFilter("billstatus", QCP.in, new String[]{"A", "B"}));
        DynamicObject[] load = BusinessDataServiceHelper.load("hc95_target_costupdate", "id", qFilterList.stream().toArray(QFilter[]::new));
        if (load != null && load.length >= 1) {
            billShowParameter.setPkId(load[0].getPkValue());
            if (load.length >= 2) {
                throw new KDBizException("目标成本变更单数据异常找到两个及两个以上未完成目标成本变更单!");
            }
            isExitPk = true;
        }
        billShowParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
        billShowParameter.setFormId("hc95_target_costupdate");
        billShowParameter.setCustomParam("sourceBill", "TargetCost");
        billShowParameter.setCustomParam("pk", billModel.getPKValue());
        billShowParameter.setCustomParam("selectEntryIds", selectEntryIdsDis);
        billShowParameter.setCustomParam("isExitPk", isExitPk);
        getView().showForm(billShowParameter);
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        String actionId = closedCallBackEvent.getActionId();
        if (actionId.equals("dofastaddline")) {
            MulBasedataDynamicObjectCollection costType = (MulBasedataDynamicObjectCollection) closedCallBackEvent.getReturnData();
            if (costType != null && costType.size() >= 1) {
                // 把树形全部加载出来
                List<DynamicObject> allTreeCostType = new ArrayList<>();
                for (DynamicObject costTypeRow : costType) {
                    DynamicObject costTypeRowObject = costTypeRow.getDynamicObject("fbasedataid");
                    Object pkValue = costTypeRowObject.getPkValue();
                    costTypeRowObject = BusinessDataServiceHelperExt.loadSingle(pkValue, "hc95_costtype_tree");
                    iterationCostType(costTypeRowObject, allTreeCostType);
                }
                List<Integer> levels = allTreeCostType.stream().map(m1 -> m1.getInt("level")).collect(Collectors.toList());
                Integer levelMax = Collections.max(levels);
                DynamicObjectCollection entryentityColl = this.getModel().getEntryEntity("hc95_tree_entryentity");
                if (allTreeCostType.size() >= 1) {
                    for (int i = 1; i <= levelMax; i++) {
                        String finalI = i + "";
                        List<DynamicObject> level = allTreeCostType.stream().filter(f1 -> f1.getString("level").equals(finalI)).collect(Collectors.toList());
                        for (DynamicObject addRow : level) {
                            String currentCostTypeId = addRow.getPkValue().toString();
                            entryentityColl = this.getModel().getEntryEntity("hc95_tree_entryentity");
                            Optional<DynamicObject> firstCheck = entryentityColl.stream().filter(f1 -> f1.getString("hc95_cost_item_en.id").equals(currentCostTypeId)).findFirst();
                            if (!firstCheck.isPresent()) {//判断是否已经存在 已经存在跳过
                                if (i == 1) {
                                    int newRowIndex = this.getModel().createNewEntryRow("hc95_tree_entryentity");
                                    this.getModel().setValue("hc95_cost_item_en", addRow, newRowIndex);
                                } else {
                                    DynamicObject parent = addRow.getDynamicObject("parent");
                                    if (parent != null) {
                                        String parentCostTypeId = parent.getPkValue().toString();
                                        //找父级所在行号
                                        entryentityColl = this.getModel().getEntryEntity("hc95_tree_entryentity");
                                        Optional<DynamicObject> first = entryentityColl.stream().filter(f1 -> f1.getString("hc95_cost_item_en.id").equals(parentCostTypeId)).findFirst();
                                        if (first.isPresent()) {
                                            int newRowIndex = this.getModel().insertEntryRow("hc95_tree_entryentity", first.get().getInt("seq") - 1);
                                            this.getModel().setValue("hc95_cost_item_en", addRow, newRowIndex);
                                        }

                                    }
                                }
                            }
                        }
                        entryentityColl = this.getModel().getEntryEntity("hc95_tree_entryentity");
                        this.getModel().updateEntryCache(entryentityColl);
                    }
                }
            }
        }
        super.closedCallBack(closedCallBackEvent);
    }

    public void iterationCostType(DynamicObject costTypeRowObject, List<DynamicObject> allTreeCostType) {
        if (costTypeRowObject != null) {
            Object pk = costTypeRowObject.getPkValue();
            boolean present = allTreeCostType.stream().filter(f1 -> f1.getPkValue().equals(pk)).findFirst().isPresent();
            if (!present) {
                allTreeCostType.add(costTypeRowObject);
            }
            DynamicObject parent = costTypeRowObject.getDynamicObject("parent");
            if (parent != null) {
                DynamicObject costTypeRowObjectParent = BusinessDataServiceHelperExt.loadSingle(parent.getPkValue(), "hc95_costtype_tree");
                iterationCostType(costTypeRowObjectParent, allTreeCostType);
            }
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        onlyFiledLock();
        handleSumRow();
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String propName = e.getProperty().getName();
        if ("hc95_cost_item_en".equals(propName)) {//目标成本项
            int rowIndex = e.getChangeSet()[0].getRowIndex();
            DynamicObjectCollection entryentity = this.getModel().getEntryEntity("hc95_tree_entryentity");
            DynamicObject rowCurrent = entryentity.get(rowIndex);
            DynamicObject oldValue = (DynamicObject) e.getChangeSet()[0].getOldValue();
            DynamicObject newValue = (DynamicObject) e.getChangeSet()[0].getNewValue();
            if (oldValue != newValue) {
                if (entryentity != null) {
                    String hc95_longid_en = rowCurrent.getString("hc95_longid_en");
                    List<DynamicObject> collChilde = entryentity.stream().filter(f1 -> f1.getString("hc95_longid_en").startsWith(hc95_longid_en + ".")).collect(Collectors.toList());
                    collChilde.forEach(item -> {
                        item.set("hc95_cost_item_en", null);
                        int seq = item.getInt("seq") - 1;
                        this.getView().updateView("hc95_cost_item_en", seq);
                    });
                }

            }
        }
        if ("hc95_project_number".equals(propName)) {
            this.getModel().beginInit();
            DynamicObject oldValue = (DynamicObject) e.getChangeSet()[0].getOldValue();
            DynamicObject newValue = (DynamicObject) e.getChangeSet()[0].getNewValue();
            this.getModel().deleteEntryData("hc95_building_all");
            if (newValue != null) {
                Object pkValue = newValue.getPkValue();
                DynamicObject ecProject = BusinessDataServiceHelper.loadSingle(pkValue, "ec_project");
                DynamicObjectCollection building = ecProject.getDynamicObjectCollection("hc95_building");
                int buildingSize = building.size();
                if (buildingSize > 0) {
                    this.getModel().batchCreateNewEntryRow("hc95_building_all", buildingSize);
                    for (int i = 0; i < building.size(); i++) {
                        DynamicObject item = building.get(i);
                        Object buildingId = item.getPkValue();
                        String hc95_building_number = item.getString("hc95_building_number");
                        String hc95_building_name = item.getString("hc95_building_name");
                        boolean hc95_enable_ld = item.getBoolean("hc95_enable_ld");
                        this.getModel().setValue("hc95_building_codeall", hc95_building_number, i);
                        this.getModel().setValue("hc95_building_nameall", hc95_building_name, i);
                        this.getModel().setValue("hc95_sourceid_ld", buildingId, i);
                        DynamicObjectCollection hc95_building_dx = item.getDynamicObjectCollection("hc95_building_dx");
                        DynamicObjectCollection hc95_building_ds = item.getDynamicObjectCollection("hc95_building_ds");
                        List<Map<String, Object>> listZB = new ArrayList<>();
                        hc95_building_dx.forEach(itemDx -> {
                            Object itemDxId = itemDx.getPkValue();
                            int hc95_dx_cs = itemDx.getInt("hc95_dx_cs");
                            HashMap<String, Object> rowN = new HashMap<>();
                            rowN.put("id", itemDxId);
                            rowN.put("cs", hc95_dx_cs);
                            rowN.put("type", "地下");
                            listZB.add(rowN);
                        });
                        hc95_building_ds.forEach(itemDs -> {
                            Object itemDsId = itemDs.getPkValue();
                            int hc95_ds_cs = itemDs.getInt("hc95_ds_cs");
                            HashMap<String, Object> rowN = new HashMap<>();
                            rowN.put("id", itemDsId);
                            rowN.put("cs", hc95_ds_cs);
                            rowN.put("type", "地上");
                            listZB.add(rowN);
                        });
                        int sizeZB = listZB.size();
                        if (sizeZB > 0) {
                            DynamicObject entryentity = this.getModel().getEntryRowEntity("hc95_building_all", i);
                            DynamicObjectCollection hc95_building_zb = entryentity.getDynamicObjectCollection("hc95_building_zb");
                            for (int su = 0; su < sizeZB; su++) {
                                DynamicObject dynamicObject = hc95_building_zb.addNew();
                                dynamicObject.set("hc95_building_type", listZB.get(su).get("type"));
                                dynamicObject.set("hc95_cs_zb", listZB.get(su).get("cs"));
                                dynamicObject.set("hc95_sourceid_zb", listZB.get(su).get("id"));
                            }
                        }
                    }
                }
                this.getModel().endInit();
                this.getView().updateView("hc95_building_all");
            } else {
                this.getModel().deleteEntryData("hc95_building_all");
                this.getView().updateView("hc95_building_all");
            }
        } else if ("hc95_qty_sum_en".equals(propName) || "hc95_price_en".equals(propName) || "hc95_sl_sum_en".equals(propName)) {
            treeSum();
            onlyFiledLock();
            handleSumRow();
        }
        if ("hc95_project_number".equals(propName)) {
            DynamicObject newValue = (DynamicObject) e.getChangeSet()[0].getNewValue();
            if (newValue != null) {
                String number = newValue.getString("number");
                this.getModel().setValue("billno", number + "_启动版V1.0");
                this.getView().updateView("billno");
            } else {
                this.getModel().setValue("billno", "");
                this.getView().updateView("billno");
            }
        }
    }

    @Override
    public void beforeAddRow(BeforeAddRowEventArgs e) {
        super.beforeAddRow(e);
    }

    @Override
    public void afterAddRow(AfterAddRowEventArgs e) {
        EntryProp entryProp = e.getEntryProp();
        String entryPropName = entryProp.getName();
        super.afterAddRow(e);
        if ("hc95_tree_entryentity".equals(entryPropName)) {
            RowDataEntity[] rowDataEntities = e.getRowDataEntities();
            if (rowDataEntities != null && rowDataEntities.length >= 1) {
                for (RowDataEntity rowDataEntity : rowDataEntities) {
                    int rowIndex = rowDataEntity.getRowIndex();
                    DynamicObject dataEntity = rowDataEntity.getDataEntity();
                    String id = dataEntity.getString("id");
                    String pid = dataEntity.getString("pid");
                    if ("0".equals(pid)) {
                        getModel().setValue("hc95_rank_en", 1, rowIndex);
                        getModel().setValue("hc95_longid_en", id, rowIndex);
                    } else {
                        DynamicObjectCollection targetCost = this.getModel().getEntryEntity("hc95_tree_entryentity");
                        DynamicObject parentRowData = targetCost.stream().filter(f1 -> f1.getString("id").equals(pid)).findFirst().get();
                        int rank = parentRowData.getInt("hc95_rank_en") + 1;
                        String longidEn = parentRowData.getString("hc95_longid_en");
                        longidEn = longidEn + "." + id;
                        getModel().setValue("hc95_rank_en", rank, rowIndex);
                        getModel().setValue("hc95_longid_en", longidEn, rowIndex);
                    }
                    getView().updateView("hc95_rank_en", rowIndex);
                    getView().updateView("hc95_longid_en", rowIndex);
                }
            }
            treeSum();
            filedLock();
            handleSumRow();
        }
    }

    @Override
    public void afterDeleteRow(AfterDeleteRowEventArgs e) {
        EntryProp entryProp = e.getEntryProp();
        String entryPropName = entryProp.getName();
        super.afterDeleteRow(e);
        if ("hc95_tree_entryentity".equals(entryPropName)) {
            filedLock();
            treeSum();
            onlyFiledLock();
            handleSumRow();
        }
    }


    public void treeSum() {
        DynamicObjectCollection targetCost = this.getModel().getEntryEntity("hc95_tree_entryentity");
        if (targetCost != null && targetCost.size() >= 1) {
            List<Integer> rankEns = targetCost.stream().map(i1 -> i1.getInt("hc95_rank_en")).collect(Collectors.toList());
            Integer rankEnMax = Collections.max(rankEns);
            treeSumByIds(targetCost, rankEnMax);
        }
    }

    public void handleSumRow() {
        EntryGrid grid = this.getView().getControl("hc95_tree_entryentity");
        Map<String, String> map = new HashMap<>();
        DynamicObjectCollection targetCost = this.getModel().getEntryEntity("hc95_tree_entryentity");
        if (targetCost != null && targetCost.size() >= 1) {
            BigDecimal hc95_zxmucbe_sum_en = targetCost.stream().filter(f1 -> f1.getBoolean("hc95_is_leaf")).map(m1 -> m1.getBigDecimal("hc95_zxmucbe_sum_en")).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal hc95_se_sum_en = targetCost.stream().filter(f1 -> f1.getBoolean("hc95_is_leaf")).map(m1 -> m1.getBigDecimal("hc95_se_sum_en")).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal hc95_ysmucbe_sum_en = targetCost.stream().filter(f1 -> f1.getBoolean("hc95_is_leaf")).map(m1 -> m1.getBigDecimal("hc95_ysmucbe_sum_en")).reduce(BigDecimal.ZERO, BigDecimal::add);
            map.put("hc95_zxmucbe_sum_en", hc95_zxmucbe_sum_en.toPlainString());
            map.put("hc95_se_sum_en", hc95_se_sum_en.toPlainString());
            map.put("hc95_ysmucbe_sum_en", hc95_ysmucbe_sum_en.toPlainString());
            grid.setFloatButtomData(map);
            this.getModel().setValue("hc95_total_cost", hc95_zxmucbe_sum_en);
        } else {
            map.put("hc95_zxmucbe_sum_en", "");
            map.put("hc95_se_sum_en", "");
            map.put("hc95_ysmucbe_sum_en", "");
            grid.setFloatButtomData(map);
        }
    }

    public void treeSumByIds(DynamicObjectCollection targetCost, Integer rankEnMax) {
        if (rankEnMax <= 0) {
            return;
        }
        String rankEnMaxStr = String.valueOf(rankEnMax);
        targetCost = this.getModel().getEntryEntity("hc95_tree_entryentity");
        Map<String, List<DynamicObject>> parentGroup = targetCost.stream().filter(f1 -> rankEnMaxStr.equals(f1.getString("hc95_rank_en"))).collect(Collectors.groupingBy(x -> x.getString("pid")));
        for (String pid : parentGroup.keySet()) {
            if ("0".equals(pid)) {
                continue;
            }
            List<DynamicObject> dynamicObjects = parentGroup.get(pid);
            //汇总 最新目标成本额和税额
            BigDecimal hc95_zxmucbe_sum_en = dynamicObjects.stream().map(m1 -> m1.getBigDecimal("hc95_zxmucbe_sum_en")).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal hc95_se_sum_en = dynamicObjects.stream().map(m1 -> m1.getBigDecimal("hc95_se_sum_en")).reduce(BigDecimal.ZERO, BigDecimal::add);
            DynamicObject parentRow = targetCost.stream().filter(f1 -> f1.getString("id").equals(pid)).findFirst().get();
            int seq = parentRow.getInt("seq") - 1;
            parentRow.set("hc95_zxmucbe_sum_en", hc95_zxmucbe_sum_en);
            parentRow.set("hc95_se_sum_en", hc95_se_sum_en);
            this.getView().updateView("hc95_zxmucbe_sum_en", seq);
            this.getView().updateView("hc95_se_sum_en", seq);
        }
        rankEnMax = rankEnMax - 1;
        treeSumByIds(targetCost, rankEnMax);
    }

    public void onlyFiledLock() {
        DynamicObjectCollection targetCost = this.getModel().getEntryEntity("hc95_tree_entryentity");
        if (targetCost != null && targetCost.size() >= 1) {
            //先全部锁定
            for (int i = 0; i < targetCost.size(); i++) {
                DynamicObject targetCostRow = targetCost.get(i);
                String id = targetCostRow.getString("id");
                //判断本级id是否是别人的父级id
                List<DynamicObject> subColl = targetCost.stream().filter(f1 -> f1.getString("pid").equals(id)).collect(Collectors.toList());
                if (subColl != null && subColl.size() >= 1) {
                    //有子级
                    this.getView().setEnable(false, i, "hc95_accoun_en");
                    this.getView().setEnable(false, i, "hc95_price_en");
                    this.getView().setEnable(false, i, "hc95_unit_en");
                    this.getView().setEnable(false, i, "hc95_qty_sum_en");
                    this.getView().setEnable(false, i, "hc95_sl_sum_en");
                } else {
                    this.getView().setEnable(true, i, "hc95_accoun_en");
                    this.getView().setEnable(true, i, "hc95_price_en");
                    this.getView().setEnable(true, i, "hc95_unit_en");
                    this.getView().setEnable(true, i, "hc95_qty_sum_en");
                    this.getView().setEnable(true, i, "hc95_sl_sum_en");
                }
            }
        }
    }


    public void filedLock() {
        DynamicObjectCollection targetCost = this.getModel().getEntryEntity("hc95_tree_entryentity");
        if (targetCost != null && targetCost.size() >= 1) {
            //先全部锁定
            for (int i = 0; i < targetCost.size(); i++) {
                DynamicObject targetCostRow = targetCost.get(i);
                String id = targetCostRow.getString("id");
                String pid = targetCostRow.getString("pid");
                BigDecimal hc95_price_en = targetCostRow.getBigDecimal("hc95_price_en");
                BigDecimal hc95_qty_sum_en = targetCostRow.getBigDecimal("hc95_qty_sum_en");

                //判断本级id是否是别人的父级id
                List<DynamicObject> subColl = targetCost.stream().filter(f1 -> f1.getString("pid").equals(id)).collect(Collectors.toList());
                if (subColl != null && subColl.size() >= 1) {
                    //有子级
//                    this.getModel().setValue("hc95_ysmucbe_sum_en", null, i);
//                    this.getModel().setValue("hc95_zxmucbe_sum_en", null, i);

                    this.getView().setEnable(false, i, "hc95_accoun_en");
                    this.getView().setEnable(false, i, "hc95_price_en");
                    this.getView().setEnable(false, i, "hc95_unit_en");
                    this.getView().setEnable(false, i, "hc95_qty_sum_en");
                    this.getView().setEnable(false, i, "hc95_sl_sum_en");

                    this.getModel().setValue("hc95_accoun_en", null, i);
                    this.getModel().setValue("hc95_price_en", null, i);
                    this.getModel().setValue("hc95_unit_en", null, i);
                    this.getModel().setValue("hc95_qty_sum_en", null, i);
                    this.getModel().setValue("hc95_sl_sum_en", null, i);
                    this.getModel().setValue("hc95_se_sum_en", null, i);
                    this.getModel().setValue("hc95_yyjezc_sum_en", null, i);
                    this.getModel().setValue("hc95_yyjeht_sum_en", null, i);
                    this.getModel().setValue("hc95_kymuebe_sum_en", null, i);
                    this.getModel().setValue("hc95_is_leaf", false, i);

                    this.getView().updateView("hc95_accoun_en", i);
                    this.getView().updateView("hc95_price_en", i);
                    this.getView().updateView("hc95_unit_en", i);
                    this.getView().updateView("hc95_qty_sum_en", i);
                    this.getView().updateView("hc95_sl_sum_en", i);
                    this.getView().updateView("hc95_se_sum_en", i);
                    this.getView().updateView("hc95_yyjezc_sum_en", i);
                    this.getView().updateView("hc95_yyjeht_sum_en", i);
                    this.getView().updateView("hc95_kymuebe_sum_en", i);
                    this.getView().updateView("hc95_is_leaf", i);

                } else {
                    this.getView().setEnable(true, i, "hc95_accoun_en");
                    this.getView().setEnable(true, i, "hc95_price_en");
                    this.getView().setEnable(true, i, "hc95_unit_en");
                    this.getView().setEnable(true, i, "hc95_qty_sum_en");
                    this.getView().setEnable(true, i, "hc95_sl_sum_en");
                    this.getModel().setValue("hc95_is_leaf", true, i);
                    if ((hc95_qty_sum_en == null || 0 == BigDecimal.ZERO.compareTo(hc95_qty_sum_en)) && (hc95_price_en == null || 0 == BigDecimal.ZERO.compareTo(hc95_price_en))) {
                        this.getModel().setValue("hc95_zxmucbe_sum_en", null, i);
                        this.getModel().setValue("hc95_se_sum_en", null, i);
                    }

                    this.getView().updateView("hc95_is_leaf", i);
                    this.getView().updateView("hc95_zxmucbe_sum_en", i);
                    this.getView().updateView("hc95_se_sum_en", i);

                }
            }
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {
        String costItemKey = "hc95_cost_item_en";
        String propertyName = beforeF7SelectEvent.getProperty().getName();
        int row = beforeF7SelectEvent.getRow();
        if (StringUtils.equals(costItemKey, propertyName)) {
            ListShowParameter showParameter = (ListShowParameter) beforeF7SelectEvent.getFormShowParameter();
            DynamicObject entryentityRow = this.getModel().getEntryRowEntity("hc95_tree_entryentity", row);
            Object pkValue = entryentityRow.getPkValue();
            Long pid = entryentityRow.getLong("pid");
            List<QFilter> allFilters = new ArrayList<QFilter>();
            if (pid != null && pid > 0) {
                DynamicObject dynamicObject = this.getModel().getEntryEntity("hc95_tree_entryentity").stream().filter(i -> i.getString("id").equals(pid + "")).findFirst().get();
                DynamicObject parentItem = dynamicObject.getDynamicObject(costItemKey);
                if (parentItem == null) {
                    this.getView().showMessage("请先选中父节点目标成本项!");
                    beforeF7SelectEvent.setCancel(true);
                    return;
                }
                int levelSub = parentItem.getInt("level") + 1;
                String parent = parentItem.getString("id");
                QFilter qFilter = new QFilter("level", QCP.equals, levelSub);
                QFilter qFilter2 = new QFilter("parent.id", QCP.equals, parent);
                allFilters.add(qFilter);
                allFilters.add(qFilter2);
            } else {
                QFilter qFilter = new QFilter("level", QCP.equals, "1");
                allFilters.add(qFilter);
            }
            showParameter.getListFilterParameter().setQFilters(allFilters);
        }
    }
}